<?php

namespace app\store\controller\apps\sharing;

use app\store\controller\Controller;
use app\store\model\sharing\Active as ActiveModel;
use app\store\model\sharing\ActiveUsers as ActiveUsersModel;
use app\store\model\sharing\Order as SharingOrderModer;
use think\Db;

/**
 * 拼单管理控制器
 * Class Active
 * @package app\store\controller\apps\sharing
 */
class Active extends Controller
{
    /**
     * 拼单列表
     * @param null $active_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($active_id = null)
    {
        $model = new ActiveModel;
        $list = $model->getList($active_id);
//        return json($list);
        return $this->fetch('index', compact('list'));
    }

    /**
     *
     * @param $active_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function users($active_id)
    {
        $model = new ActiveUsersModel;
        $list = $model->getList($active_id);
        return $this->fetch('users', compact('list'));
    }

    /**
     * 手动确认平团成功
     */
    public function confirm($active_id)
    {
        $active = ActiveModel::find($active_id);
        if (!$active) {
            $this->renderError('拼单记录异常');
        }
        Db::startTrans();
        try {
            $active->status = 20;
            $active->save();
            SharingOrderModer::where('active_id', $active_id)->update(['order_status' => 10]);
            Db::commit();
        } catch (\Exception $e) {
            DB::rollback();
        }
        return isset($e) ? $this->renderError('平团失败') : $this->renderSuccess('拼团成功');
    }
}